home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MWNUG-Sept1993 / TheDelegate.m < prev    next >
Text File  |  1995-06-12  |  5KB  |  141 lines

  1.  
  2. /*---------------------------------------------------------------------------------
  3.    A simple example of browser delegation.
  4.      
  5.      This code is free and in the public domain. See the header file for more
  6.      details.
  7.      
  8.      HISTORY
  9.      
  10.              27Sep93    DM    Finished off, getNames, fillMatrix, init 
  11.             14Sep93    DM    Initial code
  12. ---------------------------------------------------------------------------------*/
  13.  
  14.     
  15. #import <stdio.h>
  16.  
  17. #import <appkit/Matrix.h>
  18. #import <appkit/Cell.h>
  19.  
  20. #import "TheDelegate.h"
  21.  
  22. #define        NAMELENGTH    1024
  23.  
  24.  
  25. @implementation TheDelegate
  26.  
  27. - init;
  28. {
  29.   /*--------------------------------------------------------------------------------
  30.        Override to the designated initializer.  This gets called when the object is
  31.          instantiated; we do some simple stuff, like create a new instance of the 
  32.          names storage object.  That contains slots that hold up to 1024 char long
  33.          strings.
  34.          
  35.          This is the designated initializer.
  36.     --------------------------------------------------------------------------------*/
  37.     
  38.     self = [super init];
  39.     loggedOnNames = [[Storage alloc] initCount:0 
  40.                                                                      elementSize:NAMELENGTH 
  41.                                                                      description:"[1024c]"];
  42.     
  43.     return self;
  44. }
  45.     
  46.     
  47. - (int)browser                                                                    // RETURN: the number of cells loaded into the browser
  48.         :sender                                                                            // INPUT:  the browser that sent us the message
  49.         fillMatrix:matrix                                                        // INPUT:  the matrix being added to
  50.         inColumn:(int)column;                                                // INPUT:  which column of the browser is being loaded
  51. {
  52.   /*---------------------------------------------------------------------------------
  53.        The main method that loads the data.  This is the delegate method; When the 
  54.          browser is asked to load itself, it calls the browser:fillMatrix:inColumn:
  55.          method in the delegate object, namely us.  We fill the matrix in the browser
  56.          by adding rows, then return a count of the number of rows returned.
  57.     ---------------------------------------------------------------------------------*/
  58.     
  59.     Storage                    *names;                                                // another ptr to the ivar storage object
  60.     int                            i;                                                        // generic counter
  61.     NXBrowserCell        *aCell;                                                // A cell we're adding
  62.     
  63.         // get a pointer to the storage object that holds the names.  this is
  64.         // really just an alias to the ivar in the header file, so we don't
  65.         // need to worry about freeing it.
  66.         
  67.   names = [self getNames];
  68.  
  69.         // Loop through all the names, adding them to the matrix.
  70.         
  71.     for(i = 0; i < [names count]; i++)
  72.         {
  73.                 [matrix addRow];                                                                        // Create a new default cell
  74.                 aCell = [matrix cellAt:i :0];                                                // get a pointer to it
  75.                 [aCell setStringValue:(char*)[names elementAt:i]];    // set the text string
  76.                 [aCell setLeaf:YES];                                                                // Needed to avoid the messy-looking icon on the right
  77.         }
  78.             
  79.   return [names count];                                                    // How many did we add?
  80. }
  81.  
  82.  
  83.     // Private methods
  84.  
  85. -(Storage*)    getNames;                                                        // RETURN:  Storage object that contains the names of everyone logged on
  86. {
  87.   /*-------------------------------------------------------------------------------------------
  88.      This is the code that actually gets the names of everyone logged onto the machine.
  89.          It's some basic Unix stuff; we do a "popen", which executes a command-line utility,
  90.          and get back a pointer to a File.  We can then read from this file and get the 
  91.          results of the command.  I'm being lazy here about potentially really long names, but
  92.          I don't think it's going to be a problem.
  93.          
  94.          It stuffs the data we get back from the command into a Storage object, which we then pass 
  95.          back to the caller.  The caller can munge about with it at will, and not have to worry
  96.          about reading data from streams.  Sort it, do what you like with it.
  97.     -------------------------------------------------------------------------------------------*/
  98.     
  99.     FILE                *cmdResults;                                            // The file we read from to get the names of those logged in
  100.     char                fullLine[NAMELENGTH];                            // entire line we read
  101.     
  102.         // Ditch anything that might be in the names storage object, so as to prevent
  103.         // memory leaks.
  104.     
  105.     while([loggedOnNames count] > 0)
  106.             [loggedOnNames removeLastElement];
  107.         
  108.     cmdResults = popen("who", "r");                                // Open the file and run "who", which shows who's logged in
  109.     
  110.     if(!cmdResults)                                                                // for god-knows-what-reason, the attempt to run 'who' failed.
  111.         {
  112.             NXRunAlertPanel("Alert", "Could not run who" "OK", NULL, NULL, NULL);
  113.             return nil;
  114.         }
  115.  
  116.         // stuff the names into the Storage object.  The string we get back is of the form
  117.         //            Name  tty  Date (Machine Name)
  118.         // with the machine name entry optional; it only shows up on remote logins.
  119.         
  120.     while(fgets(fullLine, NAMELENGTH, cmdResults) != NULL)
  121.                 [loggedOnNames addElement:fullLine];
  122.  
  123.   return loggedOnNames;
  124. }
  125.  
  126. - refreshHit:sender;
  127. {
  128.   /*------------------------------------------------------------------------------
  129.        The button that refreshes the display of names was hit.  Reload the browser
  130.          with more recent data.
  131.          
  132.          It wouldn't be too hard to set up a thread or DPS Timed Entry to do this
  133.          automatically every few minutes
  134.      ------------------------------------------------------------------------------*/
  135.      
  136.      [theBrowser loadColumnZero];
  137.      return self;
  138. }
  139.  
  140. @end
  141.